package net.hailxenu.stats;

import java.sql.*;
import java.net.*;

public class Packet {

    private String data;
    private ClientHandler handler;
    private SqlHandler sql;

    public String PlayerName, ID;
    public int PlayerJoins, PlayerQuits, PlayerSteps, PlayerDeaths, BlocksPlaced, BlocksDestroyed, OnlineStatus, OnlineTime = 0;

    public Packet(ClientHandler ch, String s, String id)
    {
        this.data = s;
        this.ID = id.toLowerCase().trim();
        this.handler = ch;
        this.sql = this.handler.getSql();
        parse();
    }

    private void parse()
    {
        if(this.ID.equals("startupdate"))
        {

        } else
        if(this.ID.equals("update"))
        {
            String[] d = data.split(":");
            try{
                this.PlayerName = d[0].trim();
                this.OnlineStatus = Integer.parseInt(d[1]);
                this.PlayerJoins = Integer.parseInt(d[2]);
                this.PlayerQuits = Integer.parseInt(d[3]);
                this.PlayerSteps = Integer.parseInt(d[4]);
                this.PlayerDeaths = Integer.parseInt(d[5]);
                this.BlocksPlaced = Integer.parseInt(d[6]);
                this.BlocksDestroyed = Integer.parseInt(d[7]);
                if(d.length > 8)
                {
                    this.OnlineTime = Integer.parseInt(d[8]);
                }
            }catch(Exception e)
            {
                Logger.log(LT.WARNING, "Invalid packet format. Parsing may not have completed.");
                return;
            }
        }else{
            Logger.log(LT.WARNING, "Invalid packet type.");
        }
    }

    public boolean run()
    {
        if(this.ID.equals("startupdate"))
        {
            sql.write("UPDATE `players` SET `online`=0, `serverapikey`='' WHERE `serverapikey`='" + handler.APIKey + "'");
            sql.write("UPDATE `servers_players` SET `online`=0 WHERE `serverapi`='" + handler.APIKey + "'");
            return true;
        } else
        if(this.ID.equals("update"))
        {
            sql.write("INSERT INTO `servers_players` (`playerkey`, `playerName`, `serverapi`, `blocksplaced`, `blocksdestroyed`, `connects`, "
                    + "`disconnects`, `steps`, `deaths`, `online`, `onlinetime`) "
                    + "values(MD5('" + PlayerName + handler.APIKey + "'), "
                    + "'" + PlayerName +"', "
                    + "'" + handler.APIKey + "', "
                    + BlocksPlaced + ","
                    + BlocksDestroyed + ","
                    + PlayerJoins + ","
                    + PlayerQuits + ","
                    + PlayerSteps + ","
                    + PlayerDeaths + ","
                    + OnlineStatus + ","
                    + OnlineTime
                    + ") "
                    + "ON DUPLICATE KEY UPDATE "
                    + "`connects`=connects+" + PlayerJoins + ","
                    + "`disconnects`=disconnects+" + PlayerQuits + ","
                    + "`steps`=steps+" + PlayerSteps + ","
                    + "`deaths`=deaths+" + PlayerDeaths + ","
                    + "`blocksplaced`=blocksplaced+" + BlocksPlaced + ","
                    + "`blocksdestroyed`=blocksdestroyed+" + BlocksDestroyed + ","
                    + "`online`=" + OnlineStatus + ","
                    + "`onlinetime`=onlinetime+" + OnlineTime
                    + ";");
            return true;
        }
        Logger.log(LT.INFO, "Unknown packet type: " + this.ID);
        return false;
    }

    @Override
    public String toString()
    {
        String s = "";
        if(this.ID.equals("update"))
        {
         s += PlayerName + ":" + PlayerJoins + ":" + PlayerQuits + ":" + PlayerSteps +
                 ":" + PlayerDeaths + ":" + BlocksPlaced + ":" + BlocksDestroyed + ":" + OnlineStatus + ":" + OnlineTime;
        }else{
            s = this.data;
        }
        return s;
    }

}
